/**
 * Created by Administrator on 20180703.
 */
function ApiUrl(){
  var API_address;
  if(window.location.host.indexOf('localhost') > -1){
    API_address = '';
  }else {
    API_address = 'http://172.16.3.19:8080';
  }
  return API_address;
}

function Islogined() {
  var userId = localStorage.getItem('Fangaccess_token');
  if(userId == '' || userId == null ||  userId == 'undefined'){
    window.location.href = './#/login';
  }
}

export function EndTokenTime() {
  var EndTokenTime = returnId('TokenTime');
  var NewTime = new Date().getTime();
  if(NewTime <= NewTime){
    localStorage.removeItem('Fangaccess_token');
  }
}

function returnId(id) {
  var Ids = localStorage.getItem('Fang'+id);
  return Ids;
}

function formatDate(date, fmt) {
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    var o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            var str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
};

function padLeftZero(str) {
    return ('00' + str).substr(str.length);
}

// 去掉数组重复元素
function uniqueArray(data){
   data = data || [];
   var a = {};
   for (var i=0; i<data.length; i++) {
       var v = data[i];
       if (typeof(a[v]) == 'undefined'){
            a[v] = 1;
       }
   };
   data.length=0;
   for (var i in a){
        data[data.length] = i;
   }
   return data;
}
 //获取对象长度
function objLength(o){
  var t = typeof o;
  if(t == 'string'){
    return o.length;
  }else if(t == 'object'){
    var n = 0;
    for(var i in o){
      n++;
    }
    return n;
  }
  return false;
}

// 显示隐藏
function show(value){
  $(value).show();
}
function hide(value){
  $(value).fadeOut(200);
}
function textLengthFormatter(value) {  //文字过长用省略号代替
  if (value.length > 5) {
    return value.substring(0, 5) + '...';
  } else {
    return value;
  }
}


// 导出方法
export {
  ApiUrl,Islogined,returnId,formatDate,uniqueArray,objLength,show,hide,textLengthFormatter
}
